﻿using Shop.Core.Repository;
using Shop.Core.Share;
using Shop.Core.WeChat;
using Shop.IServices;
using Shop.Model.Dto.Home;
using Shop.Model.Entity.System;
using Shop.Model.View.Home;

namespace Shop.Services;

public class HomeService: IHomeService
{
    private readonly RepositoryBase<SysBanner> _bannerRepository;

    private readonly RepositoryBase<ShareLinks> _shareLinksRepository;
    
    private readonly RepositoryBase<SysArea> _areaRepository;

    private readonly WeChatService _weChatService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="bannerRepository"></param>
    /// <param name="shareLinksRepository"></param>
    /// <param name="weChatService"></param>
    /// <param name="areaRepository"></param>
    public HomeService(RepositoryBase<SysBanner> bannerRepository, 
        RepositoryBase<ShareLinks> shareLinksRepository, 
        WeChatService weChatService,
        RepositoryBase<SysArea> areaRepository)
    {
        _bannerRepository = bannerRepository;
        _shareLinksRepository = shareLinksRepository;
        _weChatService = weChatService;
        _areaRepository = areaRepository;
    }
    
    /// <summary>
    /// Banner
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public AppResult<List<SysBanner>> Banner(BannerDto dto)
    {
        var data = _bannerRepository
            .Where(x => x.Form == dto.Form && x.Equipment == dto.Equipment && x.Location == dto.Location &&
                        x.IsEnabled == true)
            .Where(x => x.StartTime == null || (x.StartTime != null && x.StartTime <= DateTime.Now))
            .Where(x => x.EndTime == null || (x.EndTime != null && x.EndTime > DateTime.Now))
            .OrderBy(x => x.Sort)
            .ToList();
        return data;
    }

    /// <summary>
    /// 获取小程序码
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<AppResult<string>> GetWxaCode(WxaCodeDto input)
    {
        using var ms = new MemoryStream();
        var links = new ShareLinks { Path = input.Scene };
        var linksId = await _shareLinksRepository.InsertReturnBigIdentityAsync(links);
        var response = await _weChatService.GetQrCode(linksId.ToString(), input.Page);
        if (response.IsSuccessful())
            return Convert.ToBase64String(response.RawBytes);
        else
        {
            if (response.ErrorCode == 41030)
                return new AppResult(400,"失败","后台小程序配置的APPID和APPSECRET对应的小程序未发布上线,或者page没有发布");
            else if (response.ErrorCode == 40001)
                return new AppResult(400, "失败", "微信小程序access_token已过期");
            else
                return new AppResult(400, "失败", $"获取小程序码失败,ErrorCode:{response.ErrorCode}");
        }
    }

    /// <summary>
    /// 获取分享路径
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<AppResult<string>> GetSharePath(long input)
    {
        var data = await _shareLinksRepository.GetFirstAsync(x => x.Id == input);
        if (data == null)
            return new AppResult(404, "失败", "参数错误");
        return data.Path;
    }

    /// <summary>
    /// 地址库
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    /// <exception cref="AreaListVo"></exception>
    public AppResult<List<AreaListVo>> AreaList(string dto)
    {
        var data = _areaRepository
            .Where(x => x.Parent_Code == dto)
            .OrderBy(x => x.Code)
            .Select(x => new AreaListVo()
            {
                Code = x.Code,
                Name = x.Name
            })
            .ToList();
        return data;
    }
}